package com.czp.wss4j;

import java.io.IOException;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;

import org.apache.ws.security.WSPasswordCallback;

public class ServerPasswordCallback implements CallbackHandler {

	@Override
	public void handle(Callback[] callbacks) throws IOException,
			UnsupportedCallbackException {
		WSPasswordCallback wspc = (WSPasswordCallback)callbacks[0];
		String id = wspc.getIdentifier();
		switch (wspc.getUsage()) {
		case WSPasswordCallback.USERNAME_TOKEN_UNKNOWN:
			
			if(!"cxf".equals(id)) {
				throw new UnsupportedCallbackException(wspc, "username is invalid.");
			}
			
			if(!"czp".equals(wspc.getPassword())) {
				throw new UnsupportedCallbackException(wspc, "password is invalid");
			}
			break;
		case WSPasswordCallback.DECRYPT:
		case WSPasswordCallback.SIGNATURE:
			if("serverKey".equals(id)) {
				wspc.setPassword("serverPassword");
			}
			break;
		}
		
	}

}
